package com.exteam.common.base;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.app.FragmentActivity;
import android.view.View;
import android.view.Window;
import android.widget.Toast;

import com.exteam.common.util.CommLogger;
import com.exteam.common.view.dialog.LoadingDialog;
import com.exteam.common.view.dialog.MessageDialog;
import com.umeng.analytics.MobclickAgent;

import java.io.IOException;
import java.net.SocketTimeoutException;
import java.util.ArrayList;
import java.util.List;

/**
 * 父类FragmentActivity
 *
 * @author sean.yanghy
 * @version v1.0
 * @created 2014-07-06 13:56:23
 */

public class BaseFragmentActivity extends FragmentActivity {

    protected LoadingDialog loadingDialog;
    protected MessageDialog messageDialog;

    public BaseController baseController;

    protected List<BaseController> baseControllers = new ArrayList<BaseController>(3);

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        loadingDialog = new LoadingDialog(this);
        messageDialog = new MessageDialog(this);
        CommLogger.initLogger(this.getClass().getSimpleName());

    }

    public Handler getMainHandler() {
        return mainHandler;
    }

    public LoadingDialog getLoadingDialog() {
        return loadingDialog;
    }

    public MessageDialog getMessageDialog() {
        return messageDialog;
    }

    protected Handler mainHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            // TODO Auto-generated method stub
            super.handleMessage(msg);

            processMainHandleMessage(msg);
        }
    };

    public void sendMainHandlerMessage(int what, Object obj) {
        Message msg = mainHandler.obtainMessage();
        msg.what = what;
        msg.obj = obj;
        mainHandler.sendMessage(msg);
    }

    // 子类重写，实现自己的消息处理
    public void processMainHandleMessage(Message msg) {
        switch (msg.what) {
            case LoadingDialog.MSG_SHOW:
                if (loadingDialog != null) {
                    loadingDialog.show((String) msg.obj);
                }
                break;
            case LoadingDialog.MSG_CLOSE:
                if (loadingDialog != null) {
                    loadingDialog.dissmis();
                }
                break;
            case MessageDialog.MSG_SHOW:
                if (messageDialog != null) {
                    messageDialog.show((String) msg.obj);
                }
                break;
            case MessageDialog.MSG_CLOSE:
                if (messageDialog != null) {
                    messageDialog.dissmis();
                }
                break;
        }
        if (baseController != null)
            baseController.processMainHandleMessage(msg);
        for (BaseController baseCon : baseControllers) {
            baseCon.processMainHandleMessage(msg);
        }
    }

    // 子类重写，实现自己的异常处理
    protected void processException(IOException ioException) {
        if (ioException instanceof SocketTimeoutException) {
            // toast(R.string.connect_timeout);
        } else {
            // toast(R.string.connect_exception);
        }
        ioException.printStackTrace();
    }

    // 子类重写，实现自己的消息处理
    protected void processSubHandleMessage(Message msg) {

    }

    // 子类重写，实现自己的点击试图事件
    public void onViewClick(View v) {
        CommLogger.d("BaseFragmentActivity onViewClick");
        if (baseController != null)
            baseController.onViewClick(v);
        for (BaseController baseCon : baseControllers) {
            baseCon.onViewClick(v);
        }
    }

    // 跳转页面
    public void startDataActivity(Class toActivity) {
        startActivity(new Intent(this, toActivity));
        // overridePendingTransition(R.anim.comm_push_left_in,
        // R.anim.comm_push_left_out);
    }

    // 跳转页面
    public void startDataActivity(Intent intent) {
        startActivity(intent);
        // overridePendingTransition(R.anim.comm_push_left_in,
        // R.anim.comm_push_left_out);
    }

    // 跳转页面
    public void startDataActivityForResult(Class toActivity, int reqCode) {
        super.startActivityForResult(new Intent(this, toActivity), reqCode);
        // overridePendingTransition(R.anim.comm_push_left_in,
        // R.anim.comm_push_left_out);
    }

    public void finishDataActivity() {
        finish();
    }

    public void finishDataActivity(int resultCode) {
        finishActivity(resultCode);
    }

    public void toast(String message) {
        Toast.makeText(getApplicationContext(), message, Toast.LENGTH_SHORT)
                .show();
    }

    public void toast(int message) {
        Toast.makeText(getApplicationContext(), message, Toast.LENGTH_SHORT)
                .show();
    }


    @Override
    protected void onRestart() {
        // TODO Auto-generated method stub
        super.onRestart();
        if (baseController != null)
            baseController.onRestart();
        for (BaseController baseCon : baseControllers) {
            baseCon.onRestart();
        }
    }

    @Override
    protected void onStart() {
        // TODO Auto-generated method stub
        super.onStart();
        if (baseController != null) {
            baseController.onStart();
        }
        for (BaseController baseCon : baseControllers) {
            baseCon.onStart();
        }
    }


    @Override
    protected void onResume() {
        // TODO Auto-generated method stub
        super.onResume();
        MobclickAgent.onResume(this);
        if (baseController != null) {
            baseController.onResume();
        }
        for (BaseController baseCon : baseControllers) {
            baseCon.onResume();
        }
    }

    @Override
    protected void onPause() {
        // TODO Auto-generated method stub
        super.onPause();
        MobclickAgent.onPause(this);
        if (baseController != null) {
            baseController.onPause();
        }
        for (BaseController baseCon : baseControllers) {
            baseCon.onPause();
        }
    }


    @Override
    protected void onStop() {
        // TODO Auto-generated method stub
        super.onStop();
        if (baseController != null) {
            baseController.onStop();
        }
        for (BaseController baseCon : baseControllers) {
            baseCon.onStop();
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();

        if (baseController != null) {
            baseController.onDestroy();
            baseController.clear();
        }
        for (BaseController baseCon : baseControllers) {
            baseCon.onDestroy();
            baseCon.clear();
        }

    }


    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent arg2) {
        super.onActivityResult(requestCode, resultCode, arg2);
        if (baseController != null) {
            baseController.onActivityResult(requestCode, resultCode, arg2);
        }
        for (BaseController baseCon : baseControllers) {
            baseController.onActivityResult(requestCode, resultCode, arg2);
        }
    }

}
